<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
            text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.sub">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.sub">
                            <el-submenu v-if="subItem.sub" :index="subItem.index" :key="subItem.index">
                                <template slot="title">{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem,i) in subItem.sub" :key="i" :index="threeItem.index">
                                    {{ threeItem.title }}
                                </el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index" :key="subItem.index">
                                {{ subItem.title }}
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
    import bus from '../common/bus';
    export default {
        data() {
            return {
                collapse: false,
                items: [
                    {
                        icon: 'el-icon-lx-home',
                        index: '1',
                        title: '系统首页',
                        sub : [{
                            index : "/Home",
                            title : "系统首页"
                        }]
                    },
                    {
                        icon: 'el-icon-lx-cart',
                        index: '2',
                        title: '商品管理',
                        sub : [{
                            index : "/goodsPool",
                            title : "商品池"
                        },{
                            index : "/goodsList",
                            title : "商品列表"
                        }]
                    },
                    {
                        icon: 'el-icon-lx-calendar',
                        index: '3',
                        title: '订单管理',
                        sub : [{
                            index : "/allOrder/order",
                            title : "全部订单"
                        },{
                            index : "/allOrder/payOrder",
                            title : "已支付"
                        },{
                            index : "/allOrder/cancelingOrder",
                            title : "取消中"
                        },{
                            index : "/allOrder/cancelOrder",
                            title : "已取消"
                        }]
                    },
                    {
                        icon: 'el-icon-lx-question',
                        index: '4',
                        title: '售后管理',
                        sub : [{
                            index : "/afterSale/order",
                            title : "全部售后订单"
                        },{
                            index : "/afterSale/examining",
                            title : "审核中"
                        },{
                            index : "/afterSale/waitReturn",
                            title : "待退货"
                        },{
                            index : "/afterSale/sending",
                            title : "寄送中"
                        },{
                            index : "/afterSale/receivedExamining",
                            title : "收货审核中"
                        },{
                            index : "/afterSale/alreadyReturn",
                            title : "已退款"
                        },{
                            index : "/afterSale/reject",
                            title : "驳回"
                        },{
                            index : "/afterSale/cancel",
                            title : "已取消"
                        }]
                    },
                    {
                        icon: 'el-icon-lx-recharge',
                        index: '5',
                        title: '虚拟充值',
                        sub : [{
                            index : "/recharge/orderList",
                            title : "虚拟充值订单列表"
                        },{
                            index : "/recharge/goodsList",
                            title : "虚拟充值商品列表"
                        },{
                            index : "/recharge/switchAccount",
                            title : "账号切换"
                        },{
                            index : "/recharge/password",
                            title : "账号和密码"
                        }]
                    }
                ]
            }
        },
        computed:{
            onRoutes(){
                return this.$route.path;
            }
        },
        created(){
            this.getMenuList();
            // 通过 Event Bus 进行组件间通信，来折叠侧边栏
            bus.$on('collapse', msg => {
                this.collapse = msg;
            })
        },
        methods: {
            getMenuList(){
                this.$axios
                    .post("/admin/system/getMenuPage.json")
                    .then(res => {
                        const data = res.data;
                        if(data.success){
                            //将index类型改为string
                            data.list.forEach((ele,index)=>{
                                ele.index = ele.index + '';
                            })
                            this.items = data.list;
                        }else{
                            this.$message.error(data.message);
                        } 
                    })
                    .catch(error => {
                        // this.$message.error('系统繁忙，请稍后重试');
                    });
            }
        }
    }
</script>

<style scoped>
    .sidebar{
        display: block;
        position: absolute;
        left: 0;
        top: 70px;
        bottom:0;
        overflow-y: scroll;
    }
    .sidebar::-webkit-scrollbar{
        width: 0;
    }
    .sidebar-el-menu:not(.el-menu--collapse){
        width: 250px;
    }
    .sidebar > ul {
        height:100%;
    }
    .el-submenu .el-menu-item{
        padding-left: 55px !important;
    }
</style>
